package admin

import (
	"context"

	"server/internal/svc"
	"server/internal/types"
	"server/models"
	"server/utils"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserListLogic {
	return &UserListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserListLogic) UserList(req *types.UserListReq) (resp *types.UserListResp, err error) {
	tx := utils.Db.Model(&models.User{})
	if req.Username != "" {
		tx.Where("username LIKE ?", "%"+req.Username+"%").Or("mobile LIKE ?", "%"+req.Username+"%").Or("email LIKE ?", "%"+req.Username+"%")
	}
	if req.Status != 0 {
		tx.Where("status = ?", req.Status)
	}

	total := utils.TxCount(tx)
	if total == 0 {
		return &types.UserListResp{
			Total: total,
			List:  []types.UserListRespItem{},
		}, nil
	}
	tx = tx.Order("id desc")
	datas := make([]types.UserListRespItem, 0)
	utils.TxPage(tx, req.Page, &datas)
	return &types.UserListResp{
		Total: total,
		List:  datas,
	}, nil
}
